package com.jeasonzhao.report.engine.impl;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.report.model.ReportParam;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.ReportParamCollection;
import java.util.ArrayList;
import com.jeasonzhao.commons.xml.HTMLNode;
import com.jeasonzhao.report.engine.repo.DictRepository;
import com.jeasonzhao.report.model.ReportDict;
import com.jeasonzhao.report.model.DictItemCollection;
import com.jeasonzhao.report.model.DictItem;
import com.jeasonzhao.report.exceptions.DictException;
import com.jeasonzhao.report.engine.servlet.Constants;

public class ParameterHtmlGenerator
{
    private ParameterHtmlGenerator()
    {
    }
    private static void debug(String x)
    {
        System.out.println(x);
    }
    public static HTMLNode generateSection(Report report)
        throws DictException
    {
        ReportParamCollection coll = null == report ? null : report.getNonFixedParameters();
        if(null == coll)
        {
            return null;
        }
        ArrayList<ReportParamCollection> lines = new ArrayList<ReportParamCollection>();
        for(ReportParam p : coll)
        {
            ReportParamCollection c = new ReportParamCollection();
            if(p == null)
            {
                continue;
            }
            int x = p.getSelectorLine();
            if(x < 1)
            {
                c.add(p);
            }
            else
            {
                for(ReportParam p2 : coll)
                {
                    if(p2.getSelectorLine() == x)
                    {
                        coll.add(p2);
                    }

                }
            }
            lines.add(c);
        }//filter lines
        HTMLNode nodeSection=new HTMLNode.DIV();
        for(ReportParamCollection line: lines)
        {
            HTMLNode nodeLine=new HTMLNode.DIV();
            nodeLine.setCssName("line");
            for(ReportParam p :line)
            {
                HTMLNode span=new HTMLNode.SPAN();
                span.setCssName("title");
                String t=p.getTitle();
                if(t.endsWith(":")||t.endsWith("："))
                {
                    t=t.substring(0,t.length()-1);
                }
                t+=": ";
                span.setInnerText(t);
                HTMLNode content= generate(p);
                if(null!=content)
                {
                    nodeLine.addNode(span);
                    nodeLine.addNode(content);
                }
            }
            nodeSection.addNode(nodeLine);
        }
        return nodeSection;
    }

    private static HTMLNode generate(ReportParam param)
        throws DictException
    {
        if(null == param)
        {
            return null;
        }
        debug(param.getTitle());
        if(Algorithms.notEmpty(param.getDictId()))
        {
            return dict(param);
        }
        else
        {
            return textbox(param);
        }
    }

    private static HTMLNode textbox(ReportParam param)
    {
        HTMLNode.INPUT select=new HTMLNode.INPUT();
        return select;
    }

    private static HTMLNode dict(ReportParam param)
        throws DictException
    {
        String id=param.getDictId();
//        String mode=param.getSelectorValue("mode","model");
//        boolean btreeMode=mode!=null && mode.trim().equalsIgnoreCase("tree");
        ReportDict dict=DictRepository.getInstance().getDictionaryConfiguration(id);
        if(null==dict)
        {
            debug("?????>>>>"+id);
            return null;
        }
        //
        HTMLNode.SELECT select=new HTMLNode.SELECT();
           select.setCssName("easyui-combobox")
               .addAttribute("required","true");
           if(param.isSingleSelect()==false)
           {
               select.addAttribute("multiple","multiple");
           }
           select.addAttribute("name",Constants.getParameterContolName(param.getParam()));
           DictItemCollection colls=DictRepository.getInstance().items(id);
           for(DictItem item : colls)
           {
               select.addOption(Algorithms.toString(item.getKey()),item.getName());
           }
           debug(select.toString());
           return select;

        //
//        if(Algorithms.isEmpty(dict.getParentId())|| false==btreeMode)
//        {
//
//        }
//        else
//        {
//
//        }
//        return null;
    }
}
